package com.example.controller;


import com.example.pojo.Brand;
import com.example.pojo.PageResult;
import com.example.pojo.Result;
import com.example.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌管理控制器
 */
@Slf4j
@RequestMapping("/brands")
@RestController
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询品牌列表  -GET http://localhost:8881/brands/all
     * 启动数据库命令：net start mysql
     */
//    @GetMapping("all")
//    public Result list(){
//        log.info("查询品牌列表");
//        List<Brand> brandList = brandService.findAll();
//        return Result.success(brandList);
//    }
    /**
     * 分页查询品牌 -GET http://localhost:8881/brands?page=1&pageSize=10
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询品牌，当前页码为：{}，每页记录数：{}",page,pageSize);
        PageResult pageResult = brandService.page(page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 根据ID删除品牌 -DELETE http://localhost:8881/brands?brandID=1
     */
    @DeleteMapping
    public Result delete(Integer brandID) {
        log.info("根据ID删除品牌，id为：{}", brandID);
        brandService.deleteById(brandID);
        return Result.success();
    }

    /**
     * 新增品牌 -POST http://localhost:8881/brands
     * {"brandID":1,"brandName":"黄山的徽商新概念细支"}
     */
    @PostMapping
    public Result save(@RequestBody Brand brand) {
        log.info("新增品牌，品牌数据为：{}", brand);
        brandService.save(brand);
        return Result.success();
    }

    /**
     * 根据ID查询品牌 -GET http://localhost:8881/brands/1
     */
    @GetMapping("/{brandID}")
    public Result getById(@PathVariable Integer brandID) {
        log.info("根据ID查询品牌，id为：{}", brandID);
        Brand brand = brandService.getById(brandID);
        return Result.success(brand);
    }

    /**
     * 修改品牌 -PUT http://localhost:8881/brands
     * 请求参数：{“brandID”:1，"brandName":"黄山的徽商新概念细支"}
     */
    @PutMapping
    public Result update(@RequestBody Brand brand) {
        log.info("修改品牌，品牌数据为：{}", brand);
        brandService.update(brand);
        return Result.success();
    }

    /**
     * 获取品牌名称列表 -GET http://localhost:8881/brands/names
     */
    @GetMapping("/names")
    public Result getBrandNames() {
        log.info("获取品牌名称列表");
        List<String> brandNames = brandService.getBrandNames();
        return Result.success(brandNames);
    }

    /**
     * 获取品牌名称列表 -GET http://localhost:8881/brands/allnames
     */
    @GetMapping("/allnames")
    public Result getAllNames() {
        log.info("获取品牌名称列表");
        List<String> brandNames = brandService.getBrandNames();
        return Result.success(brandNames);
    }
}
